package com.laihui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import com.laihui.Constants;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2016/11/10.
 */
public class CircleProgressView extends View {

    private static final String TAG = "CircleProgressBar";

    private int mMaxProgress = 100;

    private int mProgress = 0;
    private int mStartProgress = 0;

    private  Double mApr = 0D;
    float mCircleLineStrokeWidthDp =10 ;
    int mCircleLineStrokeWidth = 10;//dp

    private final int mTxtStrokeWidth = 2;


    public Double getmApr() {
        return mApr;
    }

    public void setmApr(Double mApr) {
        this.mApr = mApr;
    }

    // 画圆所在的距形区域
    private final RectF mRectF;

    private final Paint mPaint;

    private final Context mContext;

    private String mTxtHint1;
    private int dataprogress = 0;
    private String mTxtHint2;




    public CircleProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mCircleLineStrokeWidth = Constants.dip2px(context,mCircleLineStrokeWidthDp);
        mContext = context;
        mRectF = new RectF();
        mPaint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);
        int width = this.getWidth();
        int height = this.getHeight();

        if (width != height) {
            int min = Math.min(width, height);
            width = min;
            height = min;
        }

        // 设置画笔相关属性
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.rgb(252, 87, 31));
        canvas.drawColor(Color.TRANSPARENT);
        mPaint.setStrokeWidth(mCircleLineStrokeWidth);
        mPaint.setStyle(Paint.Style.STROKE);
        // 位置
        mRectF.left = mCircleLineStrokeWidth / 2; // 左上角x
        mRectF.top = mCircleLineStrokeWidth / 2; // 左上角y
        mRectF.right = width - mCircleLineStrokeWidth / 2; // 左下角x
        mRectF.bottom = height - mCircleLineStrokeWidth / 2; // 右下角y

        //Log.e("circle",((float) mProgress / mMaxProgress) * 360+"");
        mPaint.setColor(Color.rgb(230, 230, 230));
        // 绘制圆圈，进度条背景
        canvas.drawArc(mRectF, 90, 360, false, mPaint);
        mPaint.setColor(Color.rgb(252, 87, 31));
       canvas.drawArc(mRectF, 90, ((float) mProgress / mMaxProgress) * 360, false, mPaint);



        // 绘制进度文案显示
        mPaint.setStrokeWidth(mTxtStrokeWidth);
        //String text = mProgress + "%";
        int textHeight = height / 5;
        mPaint.setTextSize(textHeight-2);
        String apr = mApr+"%";
        int textWidth = (int) mPaint.measureText(apr+"", 0, apr.length());
        mPaint.setStyle(Paint.Style.FILL);
        // mPaint.setTextSize(Constants.dip2px(getContext(),32f));
        mPaint.setColor(Color.rgb(51, 51, 51));
        canvas.drawText(apr+"", width / 2 - textWidth / 2, height *(0.38F) + textHeight / 2, mPaint);

    }

    public int getMaxProgress() {
        return mMaxProgress;
    }

    public void setMaxProgress(int maxProgress) {
        this.mMaxProgress = maxProgress;
    }

    public void setProgress(int progress) {
        this.dataprogress = progress;
        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                if (mProgress < dataprogress) {
                    handler.sendEmptyMessage(0x113);
                } else {
                    cancel();
                }
            }
        }, 0, 50);

      //  this.invalidate();
    }

    /**
     * 非ＵＩ线程调用
     */
    public void setProgressNotInUiThread(int progress) {


        this.dataprogress = progress;

        this.postInvalidate();
    }
    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0x113) {
                if(mProgress < dataprogress) {
                    mProgress++;
                    invalidate();
                }
            }
            super.handleMessage(msg);
        }

    };







    public String getmTxtHint1() {
        return mTxtHint1;
    }

    public void setmTxtHint1(String mTxtHint1) {
        this.mTxtHint1 = mTxtHint1;
    }

    public String getmTxtHint2() {
        return mTxtHint2;
    }

    public void setmTxtHint2(String mTxtHint2) {
        this.mTxtHint2 = mTxtHint2;
    }
}

